<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file = "/common/operatehistory.jsp"%>
<%@ include file="/common/visit.jsp"%>
<%
	// 查询患者的随访计划及状态
	Integer uid = token.getUid();
	// 此处应添加权限控制，是否有权限查看该患者随访方案
	Integer patientUid = input.getInt("patient_uid", -1);
	User user = userByUid(patientUid);
	if (user == null) {
		Tools.encryptPrint(out, new Message(30041, "患者不存在"));
		return;
	}
	List<Map> userProfiles = family(patientUid, hospital);
	Map profileMap = new HashMap();
	for (Map map:userProfiles) {
		Integer self = (Integer)map.get("myself");
		if (self.equals(SELF))
			profileMap.putAll(map);
	}
	Map userMap = new HashMap();
	userMap.put("name", !isEmpty(user.getName())?user.getName():!isEmpty(user.getNickName())?user.getNickName():user.getUserName());
	userMap.put("gender", user.getGender());
	userMap.put("avatar", getDefaultAvatar(user));
	userMap.put("phone", Tools.getDesc(user.getPhone(), ""));
	userMap.put("uid", user.getId());
	userMap.put("id", user.getId());
	userMap.put("age", Tools.getInt(profileMap.get("age") + "", 0));
	completeOperateCount(userMap, SECTION_PATIENT, hospital, uid);
	OperateHistory oh = new OperateHistory();
	oh.setUid(uid+"");
	oh.setHospital(hospital);
	oh.setOid(patientUid + "");
	oh.setCategory(SECTION_PATIENT);
	//query(userMap,  oh);

	userMap.remove("id");
	List disturbList = QBuilder.db("basedb").select().from("OperateHistory as o").where(new Where("o.hospital=", hospital).and("o.category=", SECTION_PATIENT).and("o.operate=", NO_DISTURB).and("o.status=", STATUS_ENABLE).and("o.uid=", uid).and("o.oid=", patientUid)).query().getResultObj();
	userMap.put("disturb", 0);
	if (!isEmpty(disturbList))
		userMap.put("disturb", 1);
	List reportAuthList = QBuilder.db("basedb").select().from("OperateHistory as o").where(new Where("o.hospital=", hospital).and("o.category=", SECTION_DOCTOR).and("o.operate=", AUTH_REPORT).and("o.status=", STATUS_ENABLE).and("o.uid=", patientUid).and("o.oid=", uid)).query().getResultObj();
	userMap.put("report_auth", 1);
	if (isEmpty(reportAuthList))
		userMap.put("report_auth", 0);
	List<String> list = QBuilder.db("basedb").select("dg.groupName").from("DoctorGroup as dg, GroupPatient as gp").where(new Where("gp.status=", STATUS_ENABLE).and("dg.status=", STATUS_ENABLE).and("dg.doctorUid=", uid).and("dg.id=gp.groupId").and("gp.patientUid=", patientUid)).query().getResultObj();
	String groupNames = "-1";
	for (String groupName:list) {
		groupNames = groupNames + "," + groupName;
	}
	groupNames = groupNames.replace("-1", "").replaceFirst(",", "");
	userMap.put("group_name", groupNames);
	userMap.put("in_hospital_number", "");
	userMap.put("bed_number", "");
	List<OperateHistory> commentList = QBuilder.db("basedb").select().from("OperateHistory as o").where(new Where("o.hospital=", hospital).and("o.category=", SECTION_DOCTOR).and("o.operate=", ATTENTION).and("o.status=", STATUS_ENABLE).and("o.uid=", patientUid).and("o.oid=", uid)).query().getResultObj();
	if (isEmpty(commentList)) {
		Tools.encryptPrint(out, new Message(30041, "这不是您的患者"));
		return;
	}
	OperateHistory attentionObj = commentList.get(0);
	userMap.put("comment", Tools.getDesc(attentionObj.getJson("comment"), ""));
	Map result = new HashMap();
	result.put("visits", queryVisit(patientUid, hospital, -1, uid));
	result.put("patient", userMap);

	msg.put("data", result);
	Tools.encryptPrint(out, msg);
%>